package TCPTest06;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.UUID;

public class MyRunnable implements Runnable{

    private Socket s;
    public  MyRunnable(Socket s){
        this.s = s;
    }
    /**
     * When an object implementing interface {@code Runnable} is used
     * to create a thread, starting the thread causes the object's
     * {@code run} method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method {@code run} is that it may
     * take any action whatsoever.
     *
     * @see Thread#run()
     */
    @Override
    public void run() {
        try {
            UUID uuid = UUID.randomUUID();
            BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
            String fN = uuid.toString() ;
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\webP\\Server\\"+fN+".txt"));
            int len;
            byte[] bytes = new byte[1024];
            while ((len =bis.read(bytes))!=-1){
                bos.write(bytes,0,len);
            }
            bos.flush();


        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //资源关闭
            try {
                s.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
